package com.tigercar.servicemanagement.controller;

import com.tigercar.common.pojo.PageResult;
import com.tigercar.servicemanagement.dto.EmployeeDTO;
import com.tigercar.servicemanagement.pojo.Employee;
import com.tigercar.servicemanagement.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询员工
     */
    @GetMapping("/search")
    public ResponseEntity<PageResult<EmployeeDTO>> findEmployee(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "1") Integer rows,
            @RequestParam(value = "key",required = false) String key){
        PageResult<EmployeeDTO> pageResult= employeeService.findEmployee(page,rows,key);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增员工
     */
    @PostMapping("/add")
    public ResponseEntity<Void> addEmployee(
            Employee employee,
            @RequestParam("deptname") List<String> deptname){
        employeeService.addEmployee(employee,deptname);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改员工
     */
    @PutMapping("/revise")
    public ResponseEntity<Void> updateEmployee(
            Employee employee,
            @RequestParam("deptname") List<String> deptname){
        employeeService.updateEmployee(employee,deptname);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
